<template>
  <div class="container">
    <div class="left-form">
      <div class="login-show">
        <div class="layer1"></div>
        <div class="layer2"></div>
        <div class="layer3"></div>
        <p class="header-text" style="padding: 2%">毕业设计双选系统</p>
        <p class="left-text" style="margin-left: 7%">毕业设计全过程管理系统，为学院、教师和学生提供高效、便捷的毕设管理服务。</p>
        <div class="collage-info">
          <p>已入驻学院：{{ countInfo.deptCount }}所</p>
          <p>已入驻学生：{{ countInfo.studentCount }} 人</p>
          <p>已入驻教师：{{ countInfo.teacherCount }}人</p>
        </div>
      </div>
    </div>
    <div class="right-form" style="margin-top: 4%">
      <div class="title-text" style="margin-right: 10%">用户登录</div>
      <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form" label-position="top">
        <el-form-item prop="userName" label="学号/工号">
          <el-input size="large" v-model="loginForm.userName" type="text"  auto-complete="off" placeholder="账号" style="height: 52px; width: 85%">
            <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码" label-width="80px">
          <el-input size="large" v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码" style="height: 52px; width: 85%" @keyup.enter="handleLogin">
            <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
          </el-input>
        </el-form-item>
        <!-- <el-form-item prop="code" v-if="captchaEnabled" label="验证码" label-width="80px">
          <el-input size="large" v-model="loginForm.code"  auto-complete="off" placeholder="验证码" style="width: 65%; height: 52px" @keyup.enter="handleLogin">
            <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item> -->
        <!-- <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox> -->
        <el-form-item style="width: 100%">
          <el-button :loading="loading" size="large" type="primary" style="width: 85%; height: 55px; align-items: center" @click.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>

          <div style="float: right" v-if="register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
          <div class="info-box">
            <el-alert type="info" :description="sysInfo" show-icon></el-alert>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'
import useUserStore from '@/store/modules/user'
import { getHomeNotice } from '@/api/system/notice'
import { getSystemInfo } from '@/api/login'
import { stripHtml } from '@/utils/html'
const userStore = useUserStore()
const route = useRoute()
const router = useRouter()
const { proxy } = getCurrentInstance()
const sysInfo = ref('教师初始密码为工号+123456；学生账号为学号后6位')
const loginForm = ref({
  userName: '',
  password: '',
  rememberMe: false,
  code: '',
  uuid: ''
})
const countInfo = ref({
  deptCount: 0,
  studentCount: 0,
  teacherCount: 0
})
const loginRules = {
  userName: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
  password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
  code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
}

const codeUrl = ref('')
const loading = ref(false)
// 验证码开关
const captchaEnabled = ref(false)
// 注册开关
const register = ref(false)
const redirect = ref(undefined)

watch(
  route,
  (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect
  },
  { immediate: true }
)

function handleLogin() {
  proxy.$refs.loginRef.validate((valid) => {
    if (valid) {
      loading.value = true
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set('userName', loginForm.value.userName, { expires: 30 })
        Cookies.set('password', encrypt(loginForm.value.password), { expires: 30 })
        Cookies.set('rememberMe', loginForm.value.rememberMe, { expires: 30 })
      } else {
        // 否则移除
        Cookies.remove('userName')
        Cookies.remove('password')
        Cookies.remove('rememberMe')
      }
      // 调用action的登录方法
      userStore
        .login(loginForm.value)
        .then(() => {
          router.push({ path: redirect.value || '/' })
        })
        .catch(() => {
          loading.value = false
          // 重新获取验证码
          if (captchaEnabled.value) {
            getCode()
          }
        })
    }
  })
}

function getCode() {
  getCodeImg().then((res) => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
    if (captchaEnabled.value) {
      codeUrl.value = res.img // 此处后端直接返回图片，不用拼接了
      loginForm.value.uuid = res.uuid
    }
  })
}

function getCookie() {
  const userName = Cookies.get('userName')
  const password = Cookies.get('password')
  const rememberMe = Cookies.get('rememberMe')
  loginForm.value = {
    userName: userName === undefined ? loginForm.value.userName : userName,
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  }
}
function getNoticeInfo() {
  getHomeNotice().then((res) => {
    sysInfo.value = stripHtml(res.data.noticeContent) || '教师初始密码为工号+123456；学生账号为学号后6位'
  })
}
function getSystem() {
  getSystemInfo().then((res) => {
    countInfo.value = res.data || {
      deptCount: 0,
      studentCount: 0,
      teacherCount: 0
    }
  })
}

getCode()
getCookie()
getNoticeInfo()
getSystem()
</script>

<style lang="scss" scoped>
@use 'sass:math';

.container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100vh;
  background-size: cover;
}

.left-form {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 70%;
  height: 100%;
}

.login-show {
  width: 94%;
  height: 94%;
  background: linear-gradient(to right, #1e88e5, #2c4a76);
  // background: #000000;
  border-radius: 25px;
  margin: 3%;
  position: relative;
  display: flex;
  flex-direction: column;
}

.right-form {
  display: flex;
  flex-direction: column;
  width: 30%;
  justify-content: center;
  align-items: center;
}

.right-form .el-form-item {
  margin-bottom: 40px; // 设置每个表单项的下间距
}

@function getShadows($n) {
  $shadows: '#{random(100)}vw #{random(100)}vh #fff';

  @for $i from 2 through $n {
    $shadows: '#{$shadows},#{random(100)}vw #{random(100)}vh #fff';
  }

  @return unquote($shadows);
}
$duration: 400s;
$count: 1000;

@for $i from 1 through 3 {
  $duration: floor(math.div($duration, 2));
  $count: floor(math.div($count, 2));

  .layer#{$i} {
    $size: #{$i}px;

    position: absolute;
    width: $size;
    height: $size;
    border-radius: 50%;
    background: #f40;
    left: 0;
    top: 0;
    box-shadow: getShadows(100);
    animation: moveUp $duration linear infinite;

    &::after {
      content: '';
      position: absolute;
      left: 0;
      top: 94vh;
      width: $size;
      height: $size;
      border-radius: inherit;
      box-shadow: inherit;
    }
  }
}

@keyframes moveUp {
  100% {
    transform: translateY(-100vh);
  }
}

@mixin header-text($font-size: 60px, $letter-spacing: 4px, $fontweight: 600) {
  display: flex;
  font-size: $font-size;
  font-weight: $fontweight;
  color: white;
  margin-left: 5%;
  letter-spacing: $letter-spacing;
}

.header-text {
  @include header-text();
  display: flex;
  flex-shrink: 0;
  height: 15%;
}

.collage-info {
  height: 10%;
  display: flex;
  flex-shrink: 0;
  justify-content: center;
  gap: 25%;
  color: white;
  font-weight: 550;
  font-size: 20px;
  margin-left: 5%;
  margin-right: 5%;
}
.left-text {
  @include header-text(16px, 2px, 400);
  display: flex;
  flex: 1;
  min-height: 0;
}

.title-text {
  display: flex;
  font-size: 32px;
  justify-content: center;
  font-weight: 550;
  letter-spacing: 4px;
  margin-bottom: 10%;
  color: var(--el-color-primary);
}

.login-form {
  display: flex;
  width: 100%;
  padding: 3%;
  flex-direction: column;
  justify-content: center;
}

.el-form-item {
  width: 100%;
}

.el-button {
  width: 80%;
  height: 55px;
  align-items: center;
}

.info-button {
  display: flex;
  width: 100%;
  margin-top: 2%;
}

.info-box {
  display: flex;
  width: 85%;
  margin-top: 8%;
}
</style>
